package service

import (
	"duoke/tables"
	"duoke/utils/orm"
	"errors"
	"github.com/gin-gonic/gin"
)

var ShortOrder = new(shortOrderService)

type shortOrderService struct{}

// 获取详情
func (s *shortOrderService) Info(value interface{}, whereStr string, field string) (*tables.ShortOrder, error) {
	user := new(tables.ShortOrder)
	db := orm.Db
	switch whereStr {
	case "id":
		db.Where("id", value)
	case "order_no":
		db.Where("order_no", value)
	case "short_video_id":
		db.Where("short_video_id", value)

	}
	has, err := db.Cols(field).Get(user)
	if err != nil {
		return nil, err
	}
	if !has {
		return nil, errors.New("短视频不存在")
	}
	return user, nil
}
func (s *shortOrderService) InfoByWhere(value interface{}, field string) (*tables.ShortOrder, error) {
	user := new(tables.ShortOrder)
	has, err := orm.Db.Where(value).Cols(field).Get(user)
	if err != nil {
		return nil, err
	}
	if !has {
		return nil, errors.New("短视频不存在")
	}
	return user, nil
}
func (s *shortOrderService) MyShortVideoInfo(c *gin.Context, id int, field string) (*tables.ShortOrder, error) {
	user, err := Request.User(c)
	if err != nil {
		return nil, err
	}
	var m tables.ShortOrder
	get, err := orm.Db.Where("short_video_id=? and user_id=?", id, user.Id).Cols(field).Get(&m)
	if err != nil {
		return nil, err
	}
	if !get {
		return nil, errors.New("用户未购买")
	}
	return &m, nil
}
